home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap04 / Paint1 / Paint1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  5.1 KB  |  214 lines

  1. //***********************************************************************
  2. //
  3. //  Paint1.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "Resource.h"
  9. #include "Paint1.h"
  10.  
  11. CMyApp myApp;
  12.  
  13. /////////////////////////////////////////////////////////////////////////
  14. // CMyApp member functions
  15.  
  16. BOOL CMyApp::InitInstance ()
  17. {
  18.     m_pMainWnd = new CMainWindow;
  19.     m_pMainWnd->ShowWindow (m_nCmdShow);
  20.     m_pMainWnd->UpdateWindow ();
  21.     return TRUE;
  22. }
  23.  
  24. /////////////////////////////////////////////////////////////////////////
  25. // CMainWindow message map and member functions
  26.  
  27. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  28.     ON_WM_PAINT ()
  29.     ON_COMMAND (IDM_FILE_NEW, OnFileNew)
  30.     ON_COMMAND (IDM_FILE_EXIT, OnFileExit)
  31.     ON_COMMAND_RANGE (IDM_WIDTH_VTHIN, IDM_WIDTH_VTHICK, OnWidth)
  32.     ON_COMMAND_RANGE (IDM_COLOR_BLACK, IDM_COLOR_WHITE, OnColor)
  33.     ON_UPDATE_COMMAND_UI (IDM_FILE_NEW, OnUpdateFileNewUI)
  34.     ON_UPDATE_COMMAND_UI_RANGE (IDM_WIDTH_VTHIN, IDM_WIDTH_VTHICK,
  35.         OnUpdateWidthUI)
  36.     ON_UPDATE_COMMAND_UI_RANGE (IDM_COLOR_BLACK, IDM_COLOR_WHITE,
  37.         OnUpdateColorUI)
  38.     ON_WM_LBUTTONDOWN ()
  39.     ON_WM_MOUSEMOVE ()
  40.     ON_WM_LBUTTONUP ()
  41. END_MESSAGE_MAP ()
  42.  
  43. CMainWindow::CMainWindow ()
  44. {
  45.     m_nColor = IDM_COLOR_RED;
  46.     m_nWidth = IDM_WIDTH_MEDIUM;
  47.     m_lineArray.SetSize (0, 64);
  48.  
  49.     CString strWndClass = AfxRegisterWndClass (
  50.         0,
  51.         myApp.LoadStandardCursor (IDC_CROSS),
  52.         (HBRUSH) (COLOR_WINDOW + 1),
  53.         myApp.LoadIcon (IDR_MAINFRAME)
  54.     );
  55.     
  56.     Create (strWndClass, "Paint1", WS_OVERLAPPEDWINDOW,
  57.         rectDefault, NULL, MAKEINTRESOURCE (IDR_MAINFRAME));
  58.  
  59.     LoadAccelTable (MAKEINTRESOURCE (IDR_MAINFRAME));
  60. }
  61.  
  62. CMainWindow::~CMainWindow ()
  63. {
  64.     DeleteAllLines ();
  65. }
  66.  
  67. void CMainWindow::OnPaint ()
  68. {
  69.     CPaintDC dc (this);
  70.     int nCount = m_lineArray.GetSize ();
  71.  
  72.     if (nCount) {
  73.         for (int i=0; i<nCount; i++)
  74.             ((CLine*) m_lineArray[i])->Draw (&dc);
  75.     }
  76. }
  77.  
  78. void CMainWindow::OnFileNew ()
  79. {
  80.     DeleteAllLines ();
  81.     Invalidate ();
  82. }
  83.  
  84. void CMainWindow::OnUpdateFileNewUI (CCmdUI* pCmdUI)
  85. {
  86.     pCmdUI->Enable (m_lineArray.GetSize ());
  87. }
  88.  
  89. void CMainWindow::OnFileExit ()
  90. {
  91.     SendMessage (WM_CLOSE, 0, 0);
  92. }
  93.  
  94. void CMainWindow::OnWidth (UINT nID)
  95. {
  96.     m_nWidth = nID;
  97. }
  98.  
  99. void CMainWindow::OnColor (UINT nID)
  100. {
  101.     m_nColor = nID;
  102. }
  103.  
  104. void CMainWindow::OnUpdateWidthUI (CCmdUI* pCmdUI)
  105. {
  106.     pCmdUI->SetCheck (pCmdUI->m_nID == m_nWidth);
  107. }
  108.  
  109. void CMainWindow::OnUpdateColorUI (CCmdUI* pCmdUI)
  110. {
  111.     pCmdUI->SetCheck (pCmdUI->m_nID == m_nColor);
  112. }
  113.  
  114. void CMainWindow::OnLButtonDown (UINT nFlags, CPoint point)
  115. {
  116.     m_ptFrom = point;
  117.     m_ptTo = point;
  118.     SetCapture ();
  119. }
  120.  
  121. void CMainWindow::OnMouseMove (UINT nFlags, CPoint point)
  122. {
  123.     if (GetCapture () == this) {
  124.         CClientDC dc (this);
  125.         InvertLine (&dc, m_ptFrom, m_ptTo);
  126.         InvertLine (&dc, m_ptFrom, point);
  127.         m_ptTo = point;
  128.     }
  129. }
  130.  
  131. void CMainWindow::OnLButtonUp (UINT nFlags, CPoint point)
  132. {
  133.     static COLORREF crColors[8] = {
  134.         RGB (  0,   0,   0),    // Black
  135.         RGB (  0,   0, 255),    // Blue
  136.         RGB (  0, 255,   0),    // Green
  137.         RGB (  0, 255, 255),    // Cyan
  138.         RGB (255,   0,   0),    // Red
  139.         RGB (255,   0, 255),    // Magenta
  140.         RGB (255, 255,   0),    // Yellow
  141.         RGB (255, 255, 255)     // White
  142.     };
  143.  
  144.     static UINT nWidths[5] = { 1, 8, 16, 24, 32 };
  145.  
  146.     if (GetCapture () == this) {
  147.         ReleaseCapture ();
  148.  
  149.         CClientDC dc (this);
  150.         InvertLine (&dc, m_ptFrom, m_ptTo);
  151.         CLine* pLine = NULL;
  152.  
  153.         try {
  154.             pLine = new CLine (m_ptFrom, m_ptTo,
  155.                 nWidths[m_nWidth - IDM_WIDTH_VTHIN],
  156.                 crColors[m_nColor - IDM_COLOR_BLACK]);
  157.  
  158.             m_lineArray.Add (pLine);
  159.             pLine->Draw (&dc);
  160.         }
  161.         catch (CMemoryException* e) {
  162.             MessageBox ("Out of memory. You must clear the " \
  163.                 "drawing area before adding more lines.", "Error",
  164.                 MB_ICONEXCLAMATION | MB_OK);
  165.  
  166.             if (pLine != NULL)
  167.                 delete pLine;
  168.             e->Delete ();   
  169.         }       
  170.     }
  171. }
  172.  
  173. void CMainWindow::InvertLine (CDC* pDC, CPoint ptFrom, CPoint ptTo)
  174. {
  175.     int nOldMode = pDC->SetROP2 (R2_NOT);
  176.  
  177.     pDC->MoveTo (ptFrom);
  178.     pDC->LineTo (ptTo);
  179.  
  180.     pDC->SetROP2 (nOldMode);
  181. }
  182.  
  183. void CMainWindow::DeleteAllLines ()
  184. {
  185.     int nCount = m_lineArray.GetSize ();
  186.  
  187.     for (int i=0; i<nCount; i++)
  188.         delete m_lineArray[i];
  189.  
  190.     m_lineArray.RemoveAll ();
  191. }
  192.  
  193. /////////////////////////////////////////////////////////////////////////
  194. // CLine member functions
  195.  
  196. CLine::CLine (CPoint ptFrom, CPoint ptTo, UINT nWidth, COLORREF crColor)
  197. {
  198.     m_ptFrom = ptFrom;
  199.     m_ptTo = ptTo;
  200.     m_nWidth = nWidth;
  201.     m_crColor = crColor;
  202. }
  203.  
  204. void CLine::Draw (CDC* pDC)
  205. {
  206.     CPen pen (PS_SOLID, m_nWidth, m_crColor);
  207.  
  208.     CPen* pOldPen = pDC->SelectObject (&pen);
  209.     pDC->MoveTo (m_ptFrom);
  210.     pDC->LineTo (m_ptTo);
  211.  
  212.     pDC->SelectObject (pOldPen);
  213. }
  214.